package com.jlkf.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.filter.OncePerRequestFilter;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jlkf.dao.TblIntegralSetMapper;
import com.jlkf.dao.TblMessageMapper;
import com.jlkf.dao.TblUserMapper;
import com.jlkf.dao.TblUserPurseInfoMapper;
import com.jlkf.dao.TblUserSignMapper;
import com.jlkf.pojo.TblIntegralSet;
import com.jlkf.pojo.TblMessage;
import com.jlkf.pojo.TblUser;
import com.jlkf.pojo.TblUserPurseInfo;
import com.jlkf.pojo.TblUserSign;
import com.jlkf.service.MessageService;
import com.platform.server.response.response.pojo.ReturnData;
import com.platform.server.util.StringTools;


@Transactional
@Service
public class MessageServiceImpl  implements MessageService{

	@Autowired
	private TblMessageMapper tblMessageMapper;
	
	@Autowired
	private TblUserSignMapper tblUserSignMapper;
	
	@Autowired
	private TblUserMapper tblUserMapper;
	
	@Autowired
	private TblIntegralSetMapper tblIntegralSetMapper;
	
	@Autowired
	private TblUserPurseInfoMapper tblUserPurseInfoMapper;
	
	@Override
	public ReturnData messageList(Long userId, Integer type, Integer pageSize, Integer pageNumber) {
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=10;
		}
		PageHelper.startPage(pageNumber, pageSize);
		
		Map<String, Object> map = new HashMap<String, Object>();
	    if(userId!=null){
	    	map.put("userId", userId);
	    }
	    if(type!=null){
	    	map.put("type", type);
	    }
		List<TblMessage> messageList = tblMessageMapper.messageList(map);
		
		PageInfo<TblMessage> info  = new PageInfo<>(messageList);
		
		return new ReturnData(messageList,(int)info.getPages(),(int)info.getTotal(), 200, "信息列表");
	}

	@Override
	public ReturnData dailySign(Long userId) {
        TblIntegralSet integralSet = tblIntegralSetMapper.selectByType(5);
        
        
        int selectNow = tblUserSignMapper.selectNow(userId);
        if(selectNow>0){
         return new ReturnData(100, "今日已签到！");
        }
		TblUserSign record = new TblUserSign();
		record.setgCreateTime(new Date());
		record.setgUserId(userId);
		record.setgVersion(1);
		int i = tblUserSignMapper.insertSelective(record);
		if(i==1){
			int CountSign = tblUserSignMapper.selectCountSign(userId);
			TblMessage record1 = new TblMessage();
			record1.setgContent("今日签到成功+"+integralSet.getIntegral()+"积分,本月累计签到"+CountSign+"次");
			record1.setgTitle("每日签到");
			record1.setgType(1);
			record1.setgUserId(userId);
			record1.setgCreatetime(new Date());
			record1.setgVersion(1);
			int i1 = tblMessageMapper.insertSelective(record1);
			if(i1==1){
				TblUser selectByPrimaryKey = tblUserMapper.selectByPrimaryKey(userId);
				TblUser record2 = new TblUser();
				record2.setgUserIntegral(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
				record2.setgId(userId);
				record2.setgUserUpdateTime(new Date());
				int u= tblUserMapper.updateByPrimaryKeySelective(record2);	
				//新增积分明细
				TblUserPurseInfo purseInfo1 = new TblUserPurseInfo();
				purseInfo1.setgContent("今日签到赠送积分");
				purseInfo1.setgCreatetime(new Date());
				purseInfo1.setgStatus(1);
				purseInfo1.setgTitle("今日签到");
				purseInfo1.setgType(2);
				purseInfo1.setgBalance(selectByPrimaryKey.getgUserIntegral()+integralSet.getIntegral());
				purseInfo1.setgUserid(userId);
				purseInfo1.setgMoney((double)integralSet.getIntegral());
				tblUserPurseInfoMapper.insertSelective(purseInfo1);
				
				int sign = new Double(purseInfo1.getgBalance()).intValue();
				TblMessage record3 = new TblMessage();
				record3.setgContent("签到获得"+integralSet.getIntegral()+"积分,现有积分"+sign);
				record3.setgTitle("积分提示");
				record3.setgType(2);
				record3.setgUserId(userId);
				record3.setgCreatetime(new Date());
				record3.setgVersion(1);
				tblMessageMapper.insertSelective(record3);
				if(u==1){
					return new ReturnData(200, "签到成功");
				}
			}
		}
		return new ReturnData(2001, "签到失败");
	}
	
	@Override
	public ReturnData isSign(Long userId) {
		// TODO Auto-generated method stub
		int selectNow = tblUserSignMapper.selectNow(userId);
        if(selectNow>0){
          return new ReturnData(1,200, "今日已签到！");
        }
        return new ReturnData(0,200, "未签到！");
	}

	@Override
	public ReturnData signList(Long userId, String month,Integer pageSize,Integer pageNumber) {
          
		Map<String, Object> map  = new HashMap<>();
		if(userId!=null){
			map.put("userId", userId);
		}
		int num = tblUserSignMapper.signList(map).size();
		if(!StringTools.isEmpty(month)){
			map.put("month", month);
		}
		if(pageNumber==null){
			pageNumber=1;
		}
		if(pageSize==null){
			pageSize=1000;
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<TblUserSign> signList = tblUserSignMapper.signList(map);
        PageInfo<TblUserSign> pageInfo = new PageInfo<>(signList);
        
        //所得积分
        map.put("title", "今日签到");
        Integer sum = tblUserPurseInfoMapper.selectSignIntegral(map);
       
        Map<String, Object> map2 = new HashMap<String,Object>();
        map2.put("signList", signList);
        map2.put("cumtrapz", sum); //累计积分
        map2.put("signDays", num); // 累计签到天数
		return new ReturnData(map2,pageInfo.getPages(),(int)pageInfo.getTotal(),200,"签到列表");
	}

	@Override
	public ReturnData deleteMessage(Long mId) {
		// TODO Auto-generated method stub
		int ret = tblMessageMapper.deleteMsg(mId);
		if(ret>0){
			return new ReturnData(200,"删除成功");
		}else{
			return new ReturnData(200,"网络出错");
		}
	}

	@Override
	public ReturnData messageByGroup(Long userId) {
		// TODO Auto-generated method stub
		List<TblMessage> list = tblMessageMapper.messageGroup(userId);
		return new ReturnData(list,200,"消息列表");
	}
}
